﻿
$.ajaxSetup({
    ajaxStart: function () { $(this).attr("disabled", true); },
    ajaxComplete: function () { $(this).attr("disabled", false); }
}); 


YAHOO.example.init = function () {
    function onButtonClick(p_oEvent) {
        switch (this.get("id")) {
            case "btnSave":
                Save();
                break;
            case "btnClose":
                Close();
                break; 
            case "btnUserID":
                SelectUserList();
                break; 
            default:
                break;
        }
    }


    YAHOO.util.Event.onContentReady("toolbar", function () {
        btnSave = new YAHOO.widget.Button("btnSave");
        btnSave.on("click", onButtonClick);
        btnClose = new YAHOO.widget.Button("btnClose");
        btnClose.on("click", onButtonClick);
        btnUnAgree = new YAHOO.widget.Button("btnUserID");
        btnUnAgree.on("click", onButtonClick);
    });
} (); 


//客户选择对应的方法和ID
var _fn, _id;
function ShowDialog(fn, id, src) {
    _fn = fn;
    _id = id;
    document.getElementById("EditUIWindow1").src = src; 
    YAHOO.example.container.dialog1.show();
}


//选择按钮弹出用户选择界面
var SelectUserList = function () {
    var getUrl = "http://" + this.location.host + "/EditWidows/ChooseList.aspx", condition = ''; //condition = 'UserID not in(select UserID from CrmUserPostionConfig)';
    getUrl = getUrl + "?r=" + Math.random() + "&kid=UserID&kname=Name&ShowCheckBox=true&entity=V_UserEmp&condition=" + decodeURI(condition);
    ShowDialog(getSelected, "btnUserID", getUrl);
}

//设置数据
var getSelected = function (v, t) {
    if (v != "") {
        $("#UserID").val(v);
    }
    $("#User_Name").val(t);
}

YAHOO.namespace("example.container");
YAHOO.util.Event.onDOMReady(function () {
    var handleSubmit = function () {
        var editwindow = document.getElementById("EditUIWindow1");
        if (editwindow) {
            if (_id == "btnUserID") {
                editwindow.contentWindow.ChooseInfo();
                YAHOO.example.container.dialog1.hide();
            }
        }
        YAHOO.example.container.dialog1.hide();
    };

    var handleCancel = function () {
        this.cancel();
    };

    var handleSuccess = function (o) {
        var response = o.responseText;
        response = response.split("<!")[0];
        document.getElementById("resp").innerHTML = response;
    };

    var handleFailure = function (o) {
        alert("Submission failed: " + o.status);
    };

    YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1",
					            { width: "650px",
					                height: "400px",
					                fixedcenter: true,
					                visible: false,
					                draggable: true,
					                close: true,
					                modal: true,
					                autofillheight: "body",
					                constraintoviewport: true,
					                buttons: [{ text: "确定", handler: handleSubmit, isDefault: true },
								            { text: "取消", handler: handleCancel}]
					            });

    YAHOO.example.container.dialog1.callback = { success: handleSuccess, failure: handleFailure };
    YAHOO.example.container.dialog1.render();

    //拖动大小dialog1
    var resize1 = new YAHOO.util.Resize("dialog1", {
        handles: ["br"],
        autoRatio: false,
        minWidth: 450,
        minHeight: 300,
        status: false
    });
    resize1.on("startResize", function (args) {
        if (this.cfg.getProperty("constraintoviewport")) {
            var D = YAHOO.util.Dom;
            var clientRegion = D.getClientRegion();
            var elRegion = D.getRegion(this.element);
            resize1.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            resize1.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
        } else {
            resize1.set("maxWidth", null);
            resize1.set("maxHeight", null);
        }

    }, YAHOO.example.container.dialog1, true);
    resize1.on("resize", function (args) {
        var panelHeight = args.height;
        this.cfg.setProperty("height", panelHeight + "px");
    }, YAHOO.example.container.dialog1, true);

});


// 设窗口最大化
var SetMaxDWH = function (dialog, width, height) {
    var w = (Y.util.Dom.getViewportWidth() - width) + "px";
    var h = (Y.util.Dom.getViewportHeight() - height) + "px";
    SetDWH(w, h, dialog);
}
// 设置对话框的大小
var SetDWH = function (width, height, dialog) {
    var elements = YAHOO.util.Dom.getElementsByClassName('bd', 'div')[0];
    var w = [],h = [];
    w.push(width);
    h.push(height);
    dialog.configWidth(null, w);
    dialog.configHeight(null, h);
    dialog.render();
    if (elements) {
        dialog.fillHeight(elements);
    }
}  
 
//刷新父窗体数据
function RefreshDataParent() {
    if (parent.NeedRefreshPage != null) {
        if (typeof (parent.NeedRefreshPage.LoadData) == "function") {
            parent.NeedRefreshPage.RefreshData();
        }
    }
}

//数据保存
var Save = function () {
    var flg = false;
    var _id = $("#ID").val();
    var _useridval = $("#UserID").val();
    if (_useridval == "" || _useridval == "00000000-0000-0000-0000-000000000000") {
        alert("请选择用户!");
        $("#User_Name").val("");
        $('#User_Name').focus();
        return;
    }

    //修改，只能选择一个人，添加可以添加多个人。
    if (_id != "" && _id != "00000000-0000-0000-0000-000000000000") {
        flg = true;
        if (_useridval.indexOf(",") != -1) {
            alert("修改信息，只能选择一个用户!");
            $("#User_Name,#UserID").val("");
            $('#User_Name').focus();
            return;
        }
    }

    var _ordernumber = $("#OrderNumber").val();
    if (_ordernumber != "") {
        var reg = /^\d+$/;
        if (!reg.test(_ordernumber)) {
            alert("排序只能填写数字!");
            $('#OrderNumber').focus();
            return;
        }
    }
    var _desc = $("#KeyDesc").val();
    var _key = $("#KeyCode").val();
    var _keytxt = $("#KeyCode").find("option:selected").text();

    _desc = (_desc == "") ? _keytxt : _desc;
    _ordernumber = (_ordernumber == "") ? 0 : _ordernumber;
    _id = (_id == "") ? "00000000-0000-0000-0000-000000000000" : _id;

    var json = "{\"ID\":\"" + _id + "\",\"UserID\":\"" + _useridval + "\",\"OrderNumber\":\"" + _ordernumber + "\",\"KeyCode\":\"" + _key + "\",\"KeyDesc\":\"" + _desc + "\",\"OPType\":\"" + (flg ? 1 : 0) + "\"}";
   
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/Crm/Customer/AjaxDepartmentUser.aspx",
        data: { Action: "UserPostionConfig", Data:escape(json)},
        success: function (data) {
            if (data != null && data.Staute == "Success") {
                alert(data.Msg);
                RefreshDataParent();
                Close();
            }
            else {
                alert(data.Msg);
            }
        },
        error: function (e) {
            return;
        }
    });

}



